package com.auction.controller;

import com.alibaba.druid.util.Utils;
import com.auction.common.core.controller.MyBaseController;
import com.auction.common.token.TokenTemplate;
import com.auction.common.utils.ReturnResult;
import com.auction.entity.User;
import com.auction.mapper.RoleMapper;
import com.auction.service.ALiMsgService;
import com.auction.service.RoleService;
import com.auction.util.Login;
import com.auction.util.PageUser;
import com.auction.service.UserService;
import com.auction.service.ValidateCodeService;
import com.auction.util.UpdatePassword;
import com.auction.util.UserSigUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Api(tags = "用户管理")
public class UserController extends MyBaseController {
    @Autowired
    private UserService userService;

    @Autowired
    private TokenTemplate tokenTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ValidateCodeService validateCodeService;

    @Autowired
    private ALiMsgService aliMsgService;
    @Autowired
    private RoleService roleService;

    @Autowired
    private UserSigUtil userSigUtil;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public ReturnResult login(@RequestBody Login login) {
        String mobile = login.getMobile();
        String password = login.getPassword();
        String code = login.getCode();
        String code_key = login.getCode_key();
        Map map =userService.selectPwd(mobile);
        if (map == null || map.isEmpty()) {
            //手机号错误
            return fail("用户手机号错误");
        }
        String pwd = (String) map.get("password");
        String pwd2 = Utils.md5(password);
        if (pwd==null&&!pwd2.equals(pwd)){
            //密码错误
            return fail("用户密码错误");
        }
        String validateCode=stringRedisTemplate.opsForValue().get("code:validate:"+code_key);
        if (!code.equals(validateCode)){
            return fail("验证码错误");
        }
//        生成userSig
        String userSig = userSigUtil.generateUserSig(String.valueOf(map.get("id")) );

        //用户账号密码正确
        Map payload = new HashMap();
        payload.put("mobile", mobile);
        payload.put("password", password);
        String token = tokenTemplate.getToken(payload);
        map.put("userSig", userSig);
        map.put("token", token);
        map.remove("password");
        return success("登录成功",map);
    }

    @GetMapping("/generateValidateCode")
    @ApiOperation("图形验证码")
    public Map<String,String> generateValidateCode() {
        return validateCodeService.generateValidateCode();
    }

    @PostMapping("/saveUser")
    @ApiOperation("用户注册")
    public ReturnResult saveUser(@RequestBody User user) {
        int res=userService.saveEntity(user);
        if (res>0){
            return success("注册成功");
        } else if(res==-1){
            return fail("手机验证码验证失败");
        } else if(res==0){
            return fail("手机号已经被注册");
        } else
            return fail("注册失败");
    }

    @PutMapping("/updateUser")
    @ApiOperation("修改用户")
    public ReturnResult updateUser(@RequestBody User user) {
        int res=userService.updateEntity(user);
        return updateResult(res);
    }

    @DeleteMapping("/deleteUser")
    @ApiOperation("删除用户")
    public ReturnResult deleteUser(int id) {
        int res=userService.deleteEntity(id);
        return deleteResult(res);
    }

    @PostMapping("/queryByPage")
    @ApiOperation("分页查找用户信息")
    public ReturnResult queryUser(@RequestBody PageUser param) {
        System.out.println(userService.queryByPage(param));
        return success("用户信息列表",userService.queryByPage(param));
    }

    @GetMapping("/getALiMsg")
    @ApiOperation("获取短信验证码")
    public ReturnResult getALiMsg(String mobile) {
        aliMsgService.sendMsg(mobile);
        return success("短信发送成功");
    }

    @PostMapping("/updateStatus")
    @ApiOperation("修改状态")
    public ReturnResult updateStatus(@RequestBody User user) {
        int res=userService.updateStatusById(user);
        return updateResult(res);
    }

    @GetMapping("/getRoles")
    @ApiOperation("获取角色信息")
    public ReturnResult getRoles(){
        return success("角色信息",roleService.getRoles());
    }

    @PostMapping("/updatePwd")
    @ApiOperation("更新用户密码")
    public ReturnResult updatePwd(@RequestBody UpdatePassword  updatePassword) {
        int rs = userService.updatePassword(updatePassword);
        if (rs == -1) return fail("手机验证码有误");
        else if (rs == 0) return fail("密码有误");
        else if (rs == 1) return success("更新密码成功");
        else return updateResult(rs);
    }

    @ApiOperation("更新用户头像")
    @PostMapping("/updatePic")
    public ReturnResult updatePic(@RequestBody User user){
        int res=userService.updatePic(user);
        return updateResult(res);
    }

    @PostMapping("/updateMobile")
    @ApiOperation("更新用户手机号")
    public ReturnResult updateMobile(@RequestBody User user){
        int res=userService.updateMobile(user);
        if (res == -1) return fail("手机验证码错误");
        return updateResult(res);
    }

    @PostMapping("/updateName")
    @ApiOperation("更新用户昵称")
    public ReturnResult updateName(@RequestBody User user){
        int res=userService.updateName(user);
        return updateResult(res);
    }
    @PostMapping("/addUser")
    @ApiOperation("添加用户")
    public ReturnResult addUser(@RequestBody User user){
        int res = userService.insertUser(user);
        return saveResult(res);
    }

    @PostMapping("/updateSex")
    @ApiOperation("更改用户性别")
    public ReturnResult updateSex(@RequestBody User user){
        return updateResult(userService.updateUserSex(user));
    }

}

